package com.sumainfor.pc.dev.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.dev.dao.RaidersHousingMapper;
import com.sumainfor.pc.dev.dao.RaidersMapper;
import com.sumainfor.pc.dev.dao.UtilsMapper;
import com.sumainfor.pc.dev.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.dev.service.RaidersHousingService;
import com.sumainfor.pc.dev.service.RaidersService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 租房攻略
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-05-05 09:49
 **/
@Service
public class RaidersServiceImpl implements RaidersService {
    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(RaidersServiceImpl.class);

    @Autowired
    RaidersMapper raidersMapper ;

    @Autowired
    UtilsMapper utilsMapper ;

    @Autowired
    RaidersHousingService raidersHousingService;

    @Autowired
    ZpcMktFeign zpcMktFeign;
    /**
    * <p>
    * 根据上下架状态查询租房攻略
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public List<Map<String,Object>> getRaidersStatus(Map<String,Object> params){
     return raidersMapper.getRaidersStatus(params);
    }

    /**
    * <p>
    * 修改租房攻略上下架状态和编辑租房攻略
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult updateRaidersStatus(Map<String,Object> params){
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("更新操作");
            System.out.println("params"+params);
           // List<String> HouInfoIdOneList=(List<String>) params.get("HouInfoIdOneList");//A查询的房源详表
            List<Map<String,Object>> HouInfoIdOneList = (List<Map<String, Object>>) params.get("HouInfoIdOneList");
            System.out.println("查询的房源详表"+HouInfoIdOneList);
            //List<String> HouInfoIdList = (List<String>) params.get("HouInfoIdList");//B传过来的房源详表编号集合
            List<Map<String,Object>> HouInfoIdList = (List<Map<String, Object>>) params.get("HouInfoIdList");
            System.out.println("传过来的房源详表编号集合"+HouInfoIdList);
            if ( params.get("HouInfoIdOneList")!=null||params.get("HouInfoIdList")!=null) {
              if (HouInfoIdOneList.size() > 0 || HouInfoIdList.size() > 0) {
                  for (Map<String,Object> HouInfoIdOne:HouInfoIdOneList) {
                      HouInfoIdOne.put("RaiId", params.get("RaiId"));
                      raidersHousingService.deleteRaidersHousing(HouInfoIdOne);
                  }
                  for (Map<String,Object> HouInfoId:HouInfoIdList) {
                      HouInfoId.put("RaiId", params.get("RaiId"));
                      raidersHousingService.insertRadHou(HouInfoId);
                  }

//                   for (String HouInfoIdOne : HouInfoIdOneList) {
//                        if (!HouInfoIdList.contains(HouInfoIdOne)) {
//                            //删除操作
//                            Map<String, Object> HouInfoIdOneMap = new HashMap<>();
//                           HouInfoIdOneMap.put("HouInfoId", HouInfoIdOne);
//                            HouInfoIdOneMap.put("RaiId", params.get("RaiId"));
//                            System.out.println("删除操作的房源" + HouInfoIdOneMap);
//                            raidersHousingService.deleteRaidersHousing(HouInfoIdOneMap);
//                        }
//                    }
//                    for (String HouInfoId : HouInfoIdList) {
//                        if (!HouInfoIdOneList.contains(HouInfoId)) {
//                           //增加操作
//                            Map<String, Object> HouInfoIdMap = new HashMap<>();
//                            HouInfoIdMap.put("HouInfoId", HouInfoId);
//                           HouInfoIdMap.put("RaiId", params.get("RaiId"));
//                            System.out.println("增加操作的房源" + HouInfoIdMap);
//                            raidersHousingService.insertRadHou(HouInfoIdMap);
//                        }
//                    }
                }
            }
            params.put("by", ContextUtils.getUserId());//操作人
            params.put("term","7");//1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            if(raidersMapper.updateRaidersStatus(params)<=0){
                result.putFailed("修改失败");
            }else{
                result.putSuccess("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
            result.putFailed("服务器繁忙");
        } finally {
            lock.unlock();
        }
        return result;
    }

    /**
    * <p>
    * 删除租房攻略
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult deleteRaiders(Map<String,Object> params){
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("删除操作");
            JsonResult getRaidersHousing = raidersHousingService.getRaidersHousing(params);
            List<String> HouInfoIdList = new ArrayList<>();
            List<Map<String,Object>> getRadHousList = (List<Map<String, Object>>) getRaidersHousing.get("result");
          if (getRadHousList==null||getRadHousList.size()==0){
              if(raidersMapper.deleteRaiders(params)<=0){
                  result.putFailed("删除失败!");
              }else{
                  result.putSuccess("删除成功");
              }
          }else{
              for (Map<String, Object> getRadHousMap : getRadHousList) {
                  if (getRadHousMap.get("HouInfoId") != null) {
                      HouInfoIdList.add(MapUtils.getString(getRadHousMap, "HouInfoId"));
                  }
              }
              if (HouInfoIdList.size()>0&&HouInfoIdList !=null){
                  for (String HouInfoId:HouInfoIdList) {
                      Map<String,Object> paramsMap = new HashMap<>();
                      paramsMap.put("HouInfoId",HouInfoId);
                      paramsMap.put("RaiId",params.get("RaiId"));
                      raidersHousingService.deleteRaidersHousing(paramsMap);
                  }
              }
              if(raidersMapper.deleteRaiders(params)<=0){
                  result.putFailed("删除失败!");
              }else{
                  result.putSuccess("删除成功");
              }
          }

        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;
    }

    /**
    * <p>
    * 创建租房攻略
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult insertRaiders(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("增加操作");
            params.put("term", "7");//创建端  1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);
            String UUID;
            Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                put("uuid", "RaiId");
                put("database", Constants.DATA_SOURCE_TCDP);
                put("surface", "tcdp_raiders");
            }});
            if(uuid!=null){
                int number=Integer.valueOf(uuid.get("uuid").toString());
                UUID= ComUtils.getLocalTrmSeqNum("Rad",number);
            }else{
                UUID=ComUtils.getLocalTrmSeqNum("Rad",0);
            }
            params.put("RaiId",UUID);
            params.put("CreationTime", ConvertDateTime.getCurrentTime());
            Integer cout = raidersMapper.insertRaiders(params);
            //创建租房攻略与房源中间表
            List<Map<String,Object>> HouInfoIdList = (List<Map<String, Object>>) params.get("HouInfoIdList");
            for (Map<String,Object> HouInfoIdMap:HouInfoIdList) {
                HouInfoIdMap.put("RaiId",params.get("RaiId"));
                raidersHousingService.insertRadHou(HouInfoIdMap);
            }
            if(cout<=0){
                result.putFailed("创建失败!");
            }else{
               // List<String> HouInfoIdList = (List<String>) params.get("HouInfoIdList");
             //   Map<String,Object> HouInfoIdMap = new HashMap<>();
                //for (String HouInfoId:HouInfoIdList) {
                   // HouInfoIdMap.put("HouInfoId",HouInfoId);
                  //  HouInfoIdMap.put("RaiId",params.get("RaiId"));
                  //  raidersHousingService.insertRadHou(HouInfoIdMap);
               // }
                result.putSuccess("创建成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;
    }
}
